<?php

  require_once "JooS/PHPackager/File/Adapter/Interface.php";

 /**
  * @package JooS
  * @subpackage PHPackager
  */

  abstract class JooS_PHPackager_File_Adapter implements JooS_PHPackager_File_Adapter_Interface {
    protected function __construct() {
    }

    /**
     * @param string $contents
     * @return JooS_PHPackager_File_Adapter
     */
    public function file_put_contents($contents) {
      file_put_contents($this->get_name(), $contents);
      return $this;
    }

    /**
     * @return string
     */
    public function file_get_contents() {
      return file_get_contents($this->get_name());
    }

    /**
     * @param JooS_PHPackager_File_Adapter_Interface $dest
     * @return JooS_PHPackager_File_Adapter
     */
    public function copy(JooS_PHPackager_File_Adapter_Interface $dest) {
      copy($this->get_name(), $dest->get_name());
      return $this;
    }

    /**
     * @return JooS_PHPackager_File_Adapter
     */
    public function include_file() {
      include($this->get_name());
      return $this;
    }

    /**
     * @param JooS_PHPackager_File_Adapter_Interface|string $name
     * @return JooS_PHPackager_File_Adapter
     */
    final public static function newInstance($name) {
      require_once "JooS/Loader.php";

      if (is_string($name)) {
        $className = JooS_Loader::getClassName(__CLASS__, $name, true);
        if (JooS_Loader::loadClass($className)) {
          $instance = new $className();
        }
        else
          self::throwException("PHPackager File Adapter class not found '$className'");
      }
      elseif ($name instanceof JooS_PHPackager_File_Adapter_Interface) {
        $instance = $name;
      }
      else {
        self::throwException("Parameter '\$name' must be a string or instance of JooS_PHPackager_File_Adapter");
      }

      return $instance;
    }

    protected static function throwException($Message, $Code = 0) {
      require_once "JooS/PHPackager/File/Adapter/Exception.php";

      throw new JooS_PHPackager_File_Adapter_Exception($Message, $Code);
    }
  }
